#include "privatedialog.h"
#include "ui_privatedialog.h"
#include "mainwindow.h"
#include "messtran.h"

PrivateDialog::PrivateDialog(QWidget *parent, QString myName, QString target) :
    QDialog(parent),
    ui(new Ui::PrivateDialog)
{
    ui->setupUi(this);
    this->myname = myName;
    this->target = target;
    this->setWindowTitle(target);
}

PrivateDialog::~PrivateDialog()
{
    delete ui;
}

void PrivateDialog::on_Send_clicked()
{
    QString inf = QString(QDateTime::currentDateTime().toString("hh:mm")+" "+myname+": "+ui->textEdit->toPlainText());
    if(inf.contains('|')){
        if(QMessageBox::critical(this, tr("Error"),tr("For some reason, do not use '|' message\nActually it's quite easy to solve it "
                                                      "but i feel little dizzy after servel days work:)")))
                    return ;
    }
    client->write(mesTran(inf,target).toLocal8Bit());
    ui->textBrowser->append(inf+'\n');
    ui->textEdit->clear();
}

void PrivateDialog::on_clean_clicked()
{
    ui->textEdit->clear();
}

void PrivateDialog::recv(QString inf){
    ui->textBrowser->append(inf+'\n');
}

void PrivateDialog::setClient(QTcpSocket *client){
    this->client = client;
}
